'''
Created on 2009-12-11

@author: Madlee
'''

import wx
from wx.grid import Grid
from wx import xrc
from kuai.kan.kantool import BasicInputInterface, show_panel_in_main, init_panel
import os.path
import kuai.kan.resource as rc 

FILE_COMBO_BOX_RC = r"""<?xml version="1.0" encoding="UTF-8"?>
<resource>
  <object class="wxPanel" subclass="FileComboBox" name="FileComboBox">
    <title>Please Choose a file</title>
    <object class="wxFlexGridSizer">
      <object class="sizeritem">
        <object class="wxComboBox"/>
        <flag>wxEXPAND|wxGROW|wxALIGN_BOTTOM</flag>
      </object>
      <object class="sizeritem">
        <object class="wxButton" name="wxID_OPEN">
          <size>20,20</size>
          <label>...</label>
        </object>
        <flag>wxEXPAND|wxGROW|wxALIGN_BOTTOM</flag>
      </object>
      <cols>2</cols>
      <rows>1</rows>
      <growablecols>0</growablecols>
      <growablerows>0</growablerows>
    </object>
  </object>
</resource>"""

FILE_LIST_CTRL_RC = r"""<?xml version="1.0" encoding="UTF-8"?>
<resource>
  <object class="wxPanel" name="FileListCtrl">
    <object class="wxFlexGridSizer">
      <object class="sizeritem">
        <object class="wxGrid"/>
        <flag>wxALL|wxEXPAND|wxGROW</flag>
        <border>5</border>
      </object>
      <object class="sizeritem">
        <object class="wxBoxSizer">
          <object class="sizeritem">
            <object class="wxButton">
              <label>&amp;Open</label>
            </object>
            <flag>wxALL</flag>
            <border>5</border>
          </object>
          <object class="sizeritem">
            <object class="wxButton">
              <label>&amp;Remove</label>
            </object>
            <flag>wxALL</flag>
            <border>5</border>
          </object>
          <object class="sizeritem">
            <object class="wxButton">
              <label>&amp;Reset</label>
            </object>
            <flag>wxALL</flag>
            <border>5</border>
          </object>
          <object class="sizeritem">
            <object class="wxCheckBox">
              <label>Re&amp;cursive</label>
            </object>
            <flag>wxALL</flag>
            <border>5</border>
          </object>
          <orient>wxVERTICAL</orient>
        </object>
        <flag>wxALIGN_CENTRE_VERTICAL</flag>
      </object>
      <cols>2</cols>
      <rows>1</rows>
      <vgap>5</vgap>
      <hgap>5</hgap>
      <growablecols>0</growablecols>
      <growablerows>0</growablerows>
    </object>
  </object>
</resource>"""

class FileComboBox(wx.Panel, BasicInputInterface):
    def __init__(self, parent, *args, **kwargs):
        init_panel(self, parent, "FileComboBox", FILE_COMBO_BOX_RC, *args, **kwargs)
        self.Bind(wx.EVT_BUTTON, self.show_open_dialog, self.Children[1])
        if 'filters' in kwargs:
            self.filters = kwargs['filters']
        else:
            self.filters = []
        if 'title' in kwargs:
            self.title =kwargs['title']
        else:
            self.title = rc.S_OPEN_FILE_DIALOG_TITLE
        
        if 'dlg_style' in kwargs:
            self.style = kwargs['dlg_style'] & ~wx.FD_MULTIPLE
        else:
            self.style = wx.FD_DEFAULT_STYLE
        
        self.combo = self.GetChildren()[0]
         
    def setup(self, data):
        if 'history' in data:
            self.combo.AppendItems(data['history'])
        if 'selected' in data:
            self.combo.SetValue(data['selected'])
            
    def collect(self, data):
        data['selected'] = self.combo.GetValue()
        data['history'] = self.combo.GetStrings()
        return data
    
    def load_pref(self, data, pref):
        if pref:
            data['history'] = pref.Read('history').split('|')
            data['selected'] = pref.Read('selected')
        else:
            data['history'] = []
            data['selected'] = ''
        return data
    
    def save_pref(self, pref, data):
        assert pref
        if 'history' in data:
            if len(data['history']) > rc.MAX_FILE_HISTORY:
                data = data[:rc.MAX_FILE_HISTORY]
            pref.Write('history', '|'.join(data['history']))
        else:
            pref.DeleteEntry('history')
        if 'selected' in data:
            pref.Write('selected', data['selected'])
        else:
            pref.DeleteEntry('selected')

    def show_open_dialog(self, event):
        selected = self.combo.GetValue()
        path, name =os.path.split(selected)
        if len(self.filters) > 0:
            wild = "|".join(self.filters)
            wild += "|" + rc.ALL_FILE_FILTER
        else:
            wild = rc.ALL_FILE_FILTER
        dlg = wx.FileDialog(self, self.title, path, selected, wild, self.style)
        if wx.ID_OK == dlg.ShowModal():
            selected = dlg.GetPath()
            self.combo.SetValue(selected)
            i = self.combo.FindString(selected)
            if i != wx.NOT_FOUND and i != 0:
                self.combo.Delete(i)
            if i != 0:
                self.combo.Insert(selected, 0)


class FileListCtrl(wx.Panel, BasicInputInterface):
    def __init__(self, parent, *args, **kwargs):
        init_panel(self, parent, "FileListCtrl", FILE_LIST_CTRL_RC, *args, **kwargs)
        self.grid = self.GetChildren()[0]
        self.grid.CreateGrid(25, 25)
        
            
            
        # self.grid = self.GetChildren()[0]
        # self.grid.CreateGrid(25, 25)
        # self.grid = self.GetChildren()[0]
        # self.grid.SetColSize(3, 200)
        # self.grid.SetRowSize(4, 45)
        # self.grid.SetCellValue(0, 0, "First cell")
        # self.grid.SetCellValue(1, 1, "Another cell")
        # self.grid.SetCellValue(2, 2, "Yet another cell")
        # self.grid.SetCellValue(3, 3, "This cell is read-only")
    
if __name__ == '__main__':
    show_panel_in_main(FileListCtrl, "test", None)
